####################################
# Script Name: Power Analysis Plots
#
# Purpose: 
#
# Author: Erica Ann Metheney
#
# Contact: data@gld.gu.se
#
# Notes: 
#
#
#
#
######################################

# NECESSARY PACKAGES
library(ggplot2)
library(dplyr)

# WORKING DIRECTORY
setwd("C:/Users/xmeter/University of Gothenburg/GLD Projects - Documents/Generative AI and Survey Development/Analyses/Power Analysis")

# DATA IMPORT
model_DE <- readRDS("C:/Users/xmeter/University of Gothenburg/GLD Projects - Documents/Generative AI and Survey Development/Analyses/Power Analysis/PowerAnalysisResults_DirectEffectModel.rds")
persona_DE <- readRDS("C:/Users/xmeter/University of Gothenburg/GLD Projects - Documents/Generative AI and Survey Development/Analyses/Power Analysis/PowerAnalysisResults_DirectEffectPersona.rds")
IE <- readRDS("C:/Users/xmeter/University of Gothenburg/GLD Projects - Documents/Generative AI and Survey Development/Analyses/Power Analysis/PowerAnalysisResults_InteractionEffect.rds")


# MODEL DIRECT EFFECT
model_DE$midpoint = (model_DE$LowerCI + model_DE$UpperCI)/2
model_DE$EffectSize = factor(model_DE$EffectSize)
model1 = subset(model_DE,alpha == 0.05)
model2= subset(model_DE,alpha != 0.05)

m1<-ggplot(data=model1, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Model Effect \nwith no adjustment for multiple testing, alpha = 0.05")

ggsave("modeleffect_regular_powerplot.png", dpi=600)

m2<-ggplot(data=model2, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Model Effect \nwith Bonferroni correction for multiple testing, alpha = 0.05/11")

ggsave("modeleffect_multiple_powerplot.png", dpi=600)

# PERSONA DIRECT EFFECT
persona_DE$midpoint = (persona_DE$LowerCI + persona_DE$UpperCI)/2
persona_DE$EffectSize = factor(persona_DE$EffectSize)
persona1 = subset(persona_DE,alpha == 0.05)
persona2= subset(persona_DE,alpha != 0.05)

p1<-ggplot(data=persona1, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Persona Effect \nwith no adjustment for multiple testing, alpha = 0.05")

ggsave("personaeffect_regular_powerplot.png", dpi=600)

p2<-ggplot(data=persona2, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Persona Effect \nwithBonferroni correction for multiple testing, alpha = 0.05/11")

ggsave("personaeffect_multiple_powerplot.png", dpi=600)

# INTERACTION EFFECT
IE$midpoint = (IE$LowerCI + IE$UpperCI)/2
IE$EffectSize = factor(IE$EffectSize)
IE1 = subset(IE,alpha == 0.05)
IE2= subset(IE,alpha != 0.05)

i1<-ggplot(data=IE1, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Interaction Effect \nwith no adjustment for multiple testing, alpha = 0.05")

ggsave("interactioneffect_regular_powerplot.png", dpi=600)

i2<-ggplot(data=IE2, aes(x=Nclusters, y=midpoint, colour=EffectSize)) + 
  geom_point() + geom_line()+ 
  geom_ribbon(aes(ymin=LowerCI, ymax=UpperCI), linetype=2, alpha=0.1) +
  geom_hline(yintercept =  0.8,linetype = 3) +
  geom_vline(xintercept = 282,linetype = 3) +
  ylim(0,1)+
  xlab("Number of Questions")+
  ylab("95% Confidence Interval of Power Estimate")+
  ggtitle("Power to Detect Interaction Effect \nwith Bonferroni correction for multiple testing, alpha = 0.05/11")


ggsave("interactioneffect_multiple_powerplot.png", dpi=600)